Custom HTTP Headers, Query Parameters যোগ করা

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) WebClient কনফিগারেশন এবং Customization |
81
81

স্প্রিং বুটে RestTemplate ব্যবহার করে Custom HTTP Headers এবং Query Parameters যোগ করতে নিচের ধাপগুলো অনুসরণ করতে পারেন:


১. Custom HTTP Headers যোগ করা

HttpHeaders এবং HttpEntity ব্যবহার:

আপনি HttpHeaders ক্লাস ব্যবহার করে কাস্টম হেডার তৈরি করতে পারেন এবং এটি HttpEntity এর মধ্যে পাস করতে হবে।

উদাহরণ:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {
    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String getWithCustomHeaders(String url) {
        // Create custom headers
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer some_token");
        headers.set("Custom-Header", "CustomValue");

        // Wrap headers in HttpEntity
        HttpEntity<String> entity = new HttpEntity<>(headers);

        // Make GET request with headers
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        return response.getBody();
    }
}

কল করা:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Client {
    @Autowired
    private ApiService apiService;

    public void getRequestWithHeaders() {
        String url = "http://example.com/api/resource";
        String response = apiService.getWithCustomHeaders(url);
        System.out.println(response);
    }
}

২. Query Parameters যোগ করা

UriComponentsBuilder ব্যবহার:

UriComponentsBuilder ব্যবহার করে ডাইনামিকভাবে Query Parameters তৈরি এবং URL এ যোগ করতে পারেন।

উদাহরণ:

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class ApiService {
    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String getWithQueryParameters(String baseUrl, String param1, String param2) {
        // Build URL with query parameters
        String url = UriComponentsBuilder.fromHttpUrl(baseUrl)
                .queryParam("param1", param1)
                .queryParam("param2", param2)
                .toUriString();

        // Make GET request
        return restTemplate.getForObject(url, String.class);
    }
}

কল করা:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Client {
    @Autowired
    private ApiService apiService;

    public void getRequestWithQueryParams() {
        String baseUrl = "http://example.com/api/resource";
        String param1 = "value1";
        String param2 = "value2";

        String response = apiService.getWithQueryParameters(baseUrl, param1, param2);
        System.out.println(response);
    }
}

৩. Custom HTTP Headers + Query Parameters একসাথে ব্যবহার

উদাহরণ:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class ApiService {
    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String getWithHeadersAndParams(String baseUrl, String param1, String param2) {
        // Create custom headers
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer some_token");
        headers.set("Custom-Header", "CustomValue");

        // Wrap headers in HttpEntity
        HttpEntity<String> entity = new HttpEntity<>(headers);

        // Build URL with query parameters
        String url = UriComponentsBuilder.fromHttpUrl(baseUrl)
                .queryParam("param1", param1)
                .queryParam("param2", param2)
                .toUriString();

        // Make GET request with headers and query parameters
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        return response.getBody();
    }
}

কল করা:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Client {
    @Autowired
    private ApiService apiService;

    public void getRequestWithHeadersAndParams() {
        String baseUrl = "http://example.com/api/resource";
        String param1 = "value1";
        String param2 = "value2";

        String response = apiService.getWithHeadersAndParams(baseUrl, param1, param2);
        System.out.println(response);
    }
}

সংক্ষেপে:

  • Custom Headers: HttpHeaders এবং HttpEntity ব্যবহার করুন।
  • Query Parameters: UriComponentsBuilder ব্যবহার করুন।
  • Headers + Query Parameters: উভয় পদ্ধতিকে একত্রে ব্যবহার করুন।

এই পদ্ধতিগুলো ব্যবহার করে আপনি আপনার স্প্রিং বুট ক্লায়েন্টে কাস্টম হেডার এবং কুয়েরি প্যারামিটার যোগ করতে পারবেন।

Content added By
Promotion